<?php
namespace Admin\Controller;
use Common\Controller\CoreController;
class CategoryController extends CoreController {
    
	public function index(){
        $CategoryModel = M('Category');

        $CategoryList = $CategoryModel->order('id desc')->page($page.',20')->select();
        $this->assign('list',$CategoryList);
        // $count = $CategoryModel->count();
        // $Page = new \Think\Page($count,20);
        // $show = $Page->show();
        $this->assign('page',$show);
        $this->assign('addText','添加分类');
        $this->assign('addNew',U('addCategory'));
        $this->assign('edit',U('editCategory'));
        $this->assign('del',U('delCategory'));
        $this->cate=$this->getCateTableHtml();
       
        $this->display();
    }

    public function addCategory(){
        if(IS_POST){
            $data = array(
                'category' => I('post.category'),
                'pid' => I('post.pid'),
                'keyword' => I('post.keyword'),
                'description' => I('post.description'),
                'status' => I('post.status',0,'intval'),
                'add_time'=>date('Y-m-d H:i:s',time()),
                );
            if (I('post.pid')==0) {
                $data['level']=1;
            }else{
                $CategoryModel = M('Category');
                $level=$CategoryModel->where("id='".I('post.pid')."'")->find();
                $data['level']=$level['level']+1;
            }
            $categoryInfo = M('Category')->where("category='".I('post.category')."'")->find();
            if(!$categoryInfo){
                $categoryId = M('Category')->add($data);

                header('Location:'.U('index'));         
            }else{
                $this->error('分类已存在');
            }
        }else{
            $this->optionStr=$this->getCateOptionHtml();
            $this->assign('cancel',U('index'));
            $this->display();  
        } 
    
    }

    public function editCategory(){
        if (IS_POST) {
            $id = I('post.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }
            $data = array(
                'category' => I('post.category'),
                'pid' => I('post.pid'),
                'keyword' => I('post.keyword'),
                'description' => I('post.description'),
                'status' => I('post.status',0,'intval'),
                'add_time'=>date('Y-m-d H:i:s',time()),
                );
            $CategoryModel = M('Category');
            $level=$CategoryModel->where("id='".I('post.pid')."'")->find();
            $data['level']=$level['level']+1;
            $categoryInfo = M('Category')->where("id=".$id)->save($data);
            header('Location:'.U('index'));         
        }else{
            $id = I('get.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }
            $CategoryModel = M('Category');
            $category = M('Category')->where("id='".I('get.id')."'")->find();
            $this->assign('category',$category);
            $this->optionStr=$this->getCateOptionHtml($category['pid']);
            $this->assign('cancel',U('index'));
            $this->display(); 
        }

    }

    public function delCategory(){
        $id = I('get.id',0,'intval');
        $pid = I('get.pid',0,'intval');
        $level = I('get.level',0,'intval');
        if($id<1){
            $this->error('错误的ID');
        }
        $CategoryModel = M('Category');
        $CategoryModel->delete($id);
        $this->success('删除成功','index');

    }

}
